package org.ptkparty.client.activity;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.ptkparty.client.ClientFactory;
import org.ptkparty.client.model.UserProxy;
import org.ptkparty.client.request.UserRequest;
import org.ptkparty.client.view.players.PlayerView;
import org.ptkparty.client.view.players.PlayersView;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class PlayersActivity implements Activity {

	private static Driver driver = GWT.create(Driver.class);

	interface Driver extends RequestFactoryEditorDriver<List<UserProxy>, ListEditor<UserProxy, PlayerView>> {
	}

	private ClientFactory clientFactory;
	private UserRequest userRequest;
	private PlayersView view;

	public PlayersActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view = clientFactory.getPlayersView();
		view.setPresenter(this);
		panel.setWidget(view);

		clientFactory.getFactory().userRequest().getAllUser().fire(new Receiver<List<UserProxy>>() {

			@Override
			public void onSuccess(List<UserProxy> response) {
				userRequest = clientFactory.getFactory().userRequest();

				driver.initialize(clientFactory.getEventBus(), clientFactory.getFactory(), view.asEditor());

				driver.edit(response, userRequest);

				view.initProvider();

				view.selectFirstUser();

				userRequest.save(view.asEditor().getList()).to(new Receiver<Void>() {

					@Override
					public void onConstraintViolation(Set<ConstraintViolation<?>> violations) {
						driver.setConstraintViolations(violations);
					}

					@Override
					public void onSuccess(Void response) {
						view.refreshList();
					}
				});
			}
		});
	}

	@Override
	public String mayStop() {
		return null;
	}

	@Override
	public void onCancel() {
	}

	@Override
	public void onStop() {
	}

	public UserProxy addUser() {
		UserProxy newUser = userRequest.create(UserProxy.class);
		newUser.setEmail("newuser@gmail.com");
		newUser.setNickname("New user");
		return newUser;
	}

	public void delete(final UserProxy selectedProxy) {
		if (selectedProxy != null) {
			if (selectedProxy.getId() != null) {
				clientFactory.getFactory().userRequest().remove().using(selectedProxy).fire(new Receiver<Void>() {

					@Override
					public void onSuccess(Void response) {
						view.removeUserEditor(selectedProxy);
					}

				});
			} else {
				view.removeUserEditor(selectedProxy);
			}
		}
	}

	public void save() {
		driver.flush().fire();
	}

}
